package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.ArrayList;
import java.util.concurrent.FutureTask;

public class Action2<T1, T2> extends Delegate<IAction2<T1, T2>> {

    public Action2(IAction2<T1, T2> delegate) {
        super(delegate);
    }

    public void invoke(T1 t1, T2 t2) throws Exception {
        for (IAction2<T1, T2> action : delegates) {
            action.invoke(t1, t2);
        }
    }

    public AsyncResult<Void> invokeAsync(T1 t1, T2 t2, @Nullable AsyncDelegateCallback callback) {
        ArrayList<FutureTask<Void>> tasks = new ArrayList<>();
        for (int i = 0; i < delegates.size(); i++) {
            tasks.add(delegates.get(i).invokeAsync(t1, t2, null));
        }

        waitTasksFinish(tasks, new Object[]{t1, t2}, callback);

        //返回tasks的浅拷贝
        return new AsyncResult<>(tasks);
    }
}
